/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilidades;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;


/**
 *
 * @author williandv
 */
public class PropiedadesManager implements Serializable{
    
    
     private static PropiedadesManager currentApplicationProperties = null;
    
     private Map<String, String> jdbcProperties = null;
     
     public PropiedadesManager(){
        this.init();
     }
     
     public static PropiedadesManager getInstance() {
        if ( currentApplicationProperties == null ) {
            currentApplicationProperties = new PropiedadesManager();
        }
        return currentApplicationProperties;
    }
    
    public void init() {           
       
        if ( this.jdbcProperties == null) {
            // Leer configuracion de conexiones a DB
            this.jdbcProperties = readProperties("..\\ConexionProp.properties");
           
        }
     
    } 
    
    /**
     * Lee las propiedades cargadas en el archivo authentication.properties
     * @return
     */
    private Map<String, String> readProperties(String path) {
        Map<String, String> applicationProperties = new HashMap<String, String>();
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getResourceAsStream(path));
            for (Enumeration en = properties.keys(); en.hasMoreElements(); ) {
                String key = (String)en.nextElement();
                applicationProperties.put(key, properties.getProperty(key));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return applicationProperties;
    }
    
     public String getJdbcProperties(String propertyName) {
        return jdbcProperties.get(propertyName);
    }
     
     public static void main(String[] args) {
        getInstance();
        for(String s: getInstance().jdbcProperties.values()){
            System.out.println("la clave es:  "+s);
        }
    }

}
